import 'package:flutter/material.dart';

/// @author wangliang at 2021/11/10

class CommonClickWidget extends StatefulWidget {
  final Function onTap;
  final bool disable;
  final Widget child;
  final bool needPressColor;

  const CommonClickWidget({
    Key? key,
    required this.onTap,
    required this.child,
    this.disable = false,
    this.needPressColor = true,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _CommonClickWidgetState();
  }
}

class _CommonClickWidgetState extends State<CommonClickWidget> {
  var _pressed = false;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: AnimatedContainer(
        duration: const Duration(microseconds: 500),
        foregroundDecoration: BoxDecoration(
          color: widget.disable
              ? Colors.transparent
              : (_pressed
                  ? (widget.needPressColor ? Colors.white.withOpacity(0.5) : Colors.transparent)
                  : Colors.transparent),
        ),
        child: widget.child,
      ),
      onTap: () {
        if (!widget.disable) {
          widget.onTap();
        }
      },
      onTapDown: (d) => setState(() => _pressed = true),
      onTapUp: (d) => setState(() => _pressed = false),
      onTapCancel: () => setState(() => _pressed = false),
    );
  }
}
